<?php

namespace App\Service\service\wechat;

use App\Model\WeChat\WechatFan;
use App\Service\interface\wechat\FansServiceInterface;
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Collection;
use Hyperf\Di\Annotation\Inject;

class FansService implements FansServiceInterface
{
    #[Inject]
    protected WechatFan $model;

    /**
     * 获取粉丝列表
     * @param array $where
     * @param string $field
     * @param array $order
     * @param int $limit
     * @return LengthAwarePaginatorInterface
     */
    public function getFansList(array $where = [], string $field = '*', array $order =['id'=>'desc'], int $limit = 10): LengthAwarePaginatorInterface
    {
        // TODO: Implement getFansList() method.
        $model = $this->model->where($where);
        foreach ($order as $key=>$value){
            $model->orderBy($key,$value);
        }
        return $model->select($field)->paginate($limit);
    }

    /**
     * 获取粉丝分页列表
     * @param array $where
     * @param int $page
     * @param int $page_size
     * @param string[] $order
     * @param string $field
     * @return Builder[]|Collection
     */
    public function getFansPageList(array $where = [], int $page = 1, int $page_size = 10, $order = ['id'=>'desc'], string $field = '*'): Collection|array
    {
        $offset = $page*$page_size;
        $model = $this->model->where($where);
        foreach ($order as $key=>$value){
            $model->orderBy($key,$value);
        }
        return $model->select($field)->offset($offset)->limit($page_size)->get();
    }

    /**
     * 为微信粉丝批量打标签
     * @param array $data
     * @return bool
     */
    public function batchTagging(array $data): bool
    {
        //调用微信接口服务器配置 修改微信端粉丝标签
        $model = $this->model;
        return $model->update(['tagid_list' => implode(',', $data['tag_id_list'])], ['openid' => ['in', $data['openid_list']]]);
    }

}